package com.todo.user.application.convert;

import com.google.common.collect.Lists;
import com.todo.common.common.enums.GenderEnum;
import com.todo.common.utils.DateUtils;
import com.todo.user.api.response.BaseUserInfoResponse;
import com.todo.user.api.response.UserAccountResponse;
import com.todo.user.application.entity.bo.*;
import com.todo.user.application.frontend.model.BatchQuerySimplyUserInfoModel;
import com.todo.user.common.enums.FriendTypeEnum;
import com.todo.user.common.enums.HolidayTypeEnum;
import com.todo.user.common.enums.TravelPlanTypeEnum;
import com.todo.user.common.enums.UserStatusEnum;
import com.todo.user.iface.frontend.request.UserDetailParam;
import com.todo.user.iface.frontend.request.UserRelationListParam;
import com.todo.user.iface.frontend.response.BaseUserDetailVO;
import com.todo.user.iface.frontend.response.UserDetailVO;
import com.todo.user.iface.frontend.response.UserRelationVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangwenxiang
 * @date 2024/4/9 13:28
 * @description
 */
public class UserInfoConvert {

    public static BaseUserDetailVO buildBaseUserDetailVOByResponse(UserInfoDetailBO userInfoDetailBO) {
        if (userInfoDetailBO == null) {
            return null;
        }
        BaseUserDetailVO baseUserDetailVO = new BaseUserDetailVO();
        baseUserDetailVO.setBackgroundImg(userInfoDetailBO.getBackgroundImg());
        baseUserDetailVO.setWatchUserId(userInfoDetailBO.getUserId());
        baseUserDetailVO.setCurrentUserId(userInfoDetailBO.getUserId());
        baseUserDetailVO.setNickName(userInfoDetailBO.getNickName());
        baseUserDetailVO.setBirthday(userInfoDetailBO.getBirthday());
        baseUserDetailVO.setGenderName(userInfoDetailBO.getGender() == null ? null : userInfoDetailBO.getGender().getDesc());
        baseUserDetailVO.setHeadImg(userInfoDetailBO.getHeadImg());
        baseUserDetailVO.setLocation(userInfoDetailBO.getLocation());
        baseUserDetailVO.setMbit(userInfoDetailBO.getMbit());
        baseUserDetailVO.setNativePlace(userInfoDetailBO.getNativePlace());
        baseUserDetailVO.setPromotion(userInfoDetailBO.getPromotion());
        baseUserDetailVO.setTravelObjective(userInfoDetailBO.getTravelObjective());
        baseUserDetailVO.setTravelPlanTypeName(userInfoDetailBO.getTravelPlanType() == null ? null : userInfoDetailBO.getTravelPlanType().getName());
        baseUserDetailVO.setHolidayTypeName(userInfoDetailBO.getHolidayType() == null ? null : userInfoDetailBO.getHolidayType().getName());
        baseUserDetailVO.setInterestList(userInfoDetailBO.getInterestList());
        return baseUserDetailVO;
    }


    public static UserInfoDetailBO convertTouserInfoDetailBO(UserInfoDetailBO userInfoDetailBO) {
        if (userInfoDetailBO == null) {
            return null;
        }
        return userInfoDetailBO.builder()
                .birthday(userInfoDetailBO.getBirthday())
                .gender(userInfoDetailBO.getGender())
                .holidayType(userInfoDetailBO.getHolidayType())
                .headImg(userInfoDetailBO.getHeadImg())
                .interestList(userInfoDetailBO.getInterestList())
                .location(userInfoDetailBO.getLocation())
                .nativePlace(userInfoDetailBO.getNativePlace())
                .promotion(userInfoDetailBO.getPromotion())
                .signature(userInfoDetailBO.getSignature())
                .travelObjective(userInfoDetailBO.getTravelObjective())
                .travelPlanType(userInfoDetailBO.getTravelPlanType())
                .userId(userInfoDetailBO.getUserId())
                .userType(userInfoDetailBO.getUserType())
                .mbit(userInfoDetailBO.getMbit())
                .nickName(userInfoDetailBO.getNickName())
                .career(userInfoDetailBO.getCareer())
                .source(userInfoDetailBO.getSource())
                .backgroundImg(userInfoDetailBO.getBackgroundImg())
                .build();
    }


    public static UserAccountResponse convertToUserAccountResponse(UserAccountBO userAccountBO) {
        if (userAccountBO == null) {
            return null;
        }
        return UserAccountResponse.builder()
                .birthday(userAccountBO.getBirthday())
                .career(userAccountBO.getCareer())
                .gender(GenderEnum.getGenderEnumByType(userAccountBO.getGender()))
                .headImg(userAccountBO.getHeadImg())
                .location(userAccountBO.getLocation())
                .nativePlace(userAccountBO.getNativePlace())
                .nickName(userAccountBO.getNickName())
                .signature(userAccountBO.getSignature())
                .status(UserStatusEnum.getByType(userAccountBO.getStatus().getType()))
                .userId(userAccountBO.getId())
                .userName(userAccountBO.getUserName())
                .mobile(userAccountBO.getMobile())
                .password(userAccountBO.getPassword())
                .source(userAccountBO.getSource())
                .mobileNationCode(userAccountBO.getMobileNationCode())
                .isMobileVerify(userAccountBO.getIsMobileVerify())
                .isReal(userAccountBO.getIsReal())
                .backgroundImg(userAccountBO.getBackgroundImg())
                .build();
    }

    public static UserDetailVO convertToUserDetailVO(UserInfoDetailBO userInfoDetailBO) {
        if (userInfoDetailBO == null) {
            return null;
        }
        return UserDetailVO.builder()
                .birthday(userInfoDetailBO.getBirthday())
                .genderName(userInfoDetailBO.getGender() == null ? null : userInfoDetailBO.getGender().getDesc())
                .nativePlace(userInfoDetailBO.getNativePlace())
                .mbit(userInfoDetailBO.getMbit())
                .travelPlanType(userInfoDetailBO.getTravelPlanType() == null ? null : userInfoDetailBO.getTravelPlanType().getType())
                .travelPlanTypeName(userInfoDetailBO.getTravelPlanType() == null ? null : userInfoDetailBO.getTravelPlanType().getName())
                .headImg(userInfoDetailBO.getHeadImg())
                .career(userInfoDetailBO.getCareer())
                .promotion(userInfoDetailBO.getPromotion())
                .location(userInfoDetailBO.getLocation())
                .holidayType(userInfoDetailBO.getHolidayType() == null ? null : userInfoDetailBO.getHolidayType().getType())
                .holidayTypeName(userInfoDetailBO.getHolidayType() == null ? null : userInfoDetailBO.getHolidayType().getName())
                .nickName(userInfoDetailBO.getNickName())
                .signature(userInfoDetailBO.getSignature())
                .interestList(userInfoDetailBO.getInterestList())
                .travelObjective(userInfoDetailBO.getTravelObjective())
                .userId(userInfoDetailBO.getUserId())
                .gender(userInfoDetailBO.getGender() == null ? null : userInfoDetailBO.getGender().getType())
                .backgroundImg(userInfoDetailBO.getBackgroundImg())
                .build();
    }

    public static UserInfoDetailBO convertToUserInfoDetailBO(UserDetailParam param) {
        if (param == null) {
            return null;
        }
        return UserInfoDetailBO.builder()
                .birthday(param.getBirthday())
                .gender(GenderEnum.getGenderEnumByType(param.getGender()))
                .promotion(param.getPromotion())
                .holidayType(HolidayTypeEnum.getByType(param.getHolidayType()))
                .interestList(param.getInterestList())
                .mbit(param.getMbit())
                .userId(param.get_user().getUserId())
                .nickName(param.getNickName())
                .signature(param.getSignature())
                .travelObjective(param.getTravelObjective())
                .travelPlanType(param.getTravelPlanType() == null ? null : TravelPlanTypeEnum.getByType(param.getTravelPlanType()))
                .nativePlace(param.getNativePlace())
                .location(param.getLocation())
                .backgroundImg(param.getBackgroundImg())
                .headImg(param.getHeadImg())
                .career(param.getCareer())
                .build();
    }


    public static FollowFriendBO convertToUserRelationListRequest(UserRelationListParam param) {
        if (param == null) {
            return null;
        }
        return FollowFriendBO.builder()
                .userId(param.get_user().getUserId())
                .relationType(FriendTypeEnum.getByType(param.getFriendType()))
                .pageIndex(param.getPageIndex())
                .pageSize(param.getPageSize())
                .currentIndex(param.getCurrentIndex())
                .build();
    }

    public static List<UserRelationVO> buildUserRelationVOList(List<FollowFriendInfoBO> results, BatchQuerySimplyUserInfoModel userInfoResponse) {
        if (CollectionUtils.isEmpty(results) || userInfoResponse == null || MapUtils.isEmpty(userInfoResponse.getSimplyUserInfoMap())) {
            return Lists.newArrayList();
        }
        List<UserRelationVO> result = new ArrayList<>();
        for (FollowFriendInfoBO response : results) {
            if (userInfoResponse.getSimplyUserInfoMap().containsKey(response.getRelationUserId())) {
                UserRelationVO userRelationVO = new UserRelationVO();
                userRelationVO.setFriendType(response.getFriendType().getName());
                userRelationVO.setOperateReason(response.getOperateReason());
                userRelationVO.setRelationDay(DateUtils.differentDaysByMillisecond(response.getCreateTime(), new Date()));
                userRelationVO.setRelationUserId(response.getRelationUserId());
                userRelationVO.setUserId(response.getUserId());
                BatchQuerySimplyUserInfoModel.SimplyUserInfo simplyUserInfo = userInfoResponse.getSimplyUserInfoMap().get(response.getRelationUserId());
                userRelationVO.setUserNikeName(simplyUserInfo.getUserNikeName());
                userRelationVO.setUserHeadImg(simplyUserInfo.getUserAvatar());
                result.add(userRelationVO);
            }

        }
        return result;
    }

    public static BaseUserInfoResponse convertToBaseUserInfoResponse(UserSimplyInfoBO userSimplyInfoBO) {
        if (userSimplyInfoBO == null) {
            return null;
        }
        BaseUserInfoResponse response = new BaseUserInfoResponse();
        response.setUserId(userSimplyInfoBO.getUserId());
        response.setUserName(userSimplyInfoBO.getUserName());
        response.setNickName(userSimplyInfoBO.getNickName());
        response.setHeadImg(userSimplyInfoBO.getHeadImg());
        response.setLocation(userSimplyInfoBO.getLocation());
        response.setGender(userSimplyInfoBO.getGender());
        return response;
    }
}
